package ppms.ui.view;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import ppms.commons.data.controller.PropertyDataTreeController;
import ppms.commons.data.entity.IDataEntity;
import ppms.ui.edtor.PropertyEditor;
import ppms.ui.edtor.PropertyEditorInput;
import ppms.ui.provider.PropertyTreeProvider;


public class PropertyTreeView extends ViewPart
{
    public static final String ID = "ppms.ui.view.PropertyTreeView"; //$NON-NLS-1$

    private Tree tree;
    
    /**
     * Create contents of the view part
     * @param parent
     */
    @Override
    public void createPartControl(Composite parent)
    {
	Composite container = new Composite(parent, SWT.NONE);
	container.setLayout(new GridLayout(1,false));
	
	final TreeViewer treeViewer = new TreeViewer(container);
	
	PropertyTreeProvider provider = new PropertyTreeProvider();
	
	this.tree = treeViewer.getTree();
	this.tree.setLayoutData(new GridData(GridData.FILL_BOTH));
	
	treeViewer.setContentProvider(provider);
	treeViewer.setLabelProvider(provider);
	treeViewer.setInput(new PropertyDataTreeController());
	treeViewer.getTree().addListener(SWT.MouseDoubleClick, new Listener(){

	    public void handleEvent(Event event)
	    {
		//打开编辑器
		Point point = new Point(event.x, event.y);
	        TreeItem item = tree.getItem(point);
	        
	        IDataEntity data = (IDataEntity)item.getData();
	        
	        try
		{
		    getViewSite().getWorkbenchWindow().getActivePage().openEditor(new PropertyEditorInput(data), PropertyEditor.ID);
		}
		catch (PartInitException e)
		{
		    e.printStackTrace();
		}
	    }});
	
	//
	createActions();
	initializeToolBar();
	initializeMenu();
    }

    /**
     * Create the actions
     */
    private void createActions()
    {
	// Create the actions
    }

    /**
     * Initialize the toolbar
     */
    private void initializeToolBar()
    {
	IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
    }

    /**
     * Initialize the menu
     */
    private void initializeMenu()
    {
	IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
    }

    @Override
    public void setFocus()
    {
	// Set the focus
    }

}
